<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\Contract;
use App\Services\ContractService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class ContractController extends Controller
{
    protected $ContractService;
    public function __construct(ContractService $ContractService)
    {
        $this->ContractService = $ContractService;
    }

    public function index(Request $Request) {
        $search = $Request->search;
        $type = $Request->type ?? 1;
        $limit = $Request->limit ?: 10;
        $page = $Request->page ?: 1;
        $page = $page * $limit - $limit;

        $sql = $this->ContractService->get($search,$type);
        $sql->where('project_id',$Request->project_id);
        $sql->where('user_id',Auth::user()->id);
        $total = $sql->count();
        $data = $sql->skip($page)->take($limit)->orderBy('created_at','desc')->get();
        foreach ($data as $k => $v) {
            $data[$k]->is_process = false;
            if($v->application){
                $data[$k]->is_process = $v->application->is_process;
            }
        }
        return response()->json(['code' => '200', 'msg' => '操作成功','total'=>$total,'data'=>$data]);
    }

    public function all(Request $Request) {
        $Contract = Contract::whereIn('type',['Material','MaterialDouble'])->where('project_id',$Request->project_id)->orderBy('created_at','desc')->get();
        $data = [];
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$Contract]);
    }

    public function show(Request $Request) {
        if(!$Request->id){
            return response()->json(['code' => '403', 'msg' => '参数错误']);
        }
        $data = Contract::with('application')->with('material_template')->with('labor_template')->with('application')->with('details')->with('user')->where('id',$Request->id)->first();
        if($data){
            $data->date = Date("Y-m-d",time());
            if($data->type == 'Labor'){
                $data->material_template = null;
            }else{
                $data->labor_template = null;
            }
        }
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function store(Request $Request)
    {
        $POSTONLY = Contract::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['user_id'] = Auth::user()->id;
        $data = $this->ContractService->insert($input,$Request->details);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function import(Request $Request) {
        $data = $Request->data;
        if(!$data){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = json_decode($data);
        foreach ($data as $k => $v) {
            if(!$v->name){
                return response()->json(['code' => '404', 'msg' => '项目名称不能为空']);
            }
        }
        $data = $this->ContractService->importInsert($data);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }


    public function update(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }

        $POSTONLY = Contract::POSTONLY;
        $input = $Request->only($POSTONLY);
        $input['user_id'] = Auth::user()->id;
        $input['id'] = $Request->id;
        $data = $this->ContractService->update($input,$Request->details);
        return response()->json(['code' => '200', 'msg' => '操作成功','data'=>$data]);
    }

    public function delete(Request $Request)
    {
        if(!$Request->id){
            return response()->json(['code' => '404', 'msg' => '请选择要操作的记录']);
        }
        $data = Contract::find($Request->id);
        $data->delete();
        return response()->json(['code' => '200', 'msg' => '操作成功']);
    }
}
